package studyguide.ch08;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ItemTypesMidlet extends MIDlet {

	private final Form form;
	private final Command exitCommand;
	private final Display display;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
		
	}
	
	private class SimpleItemStateListener implements ItemStateListener {

		public void itemStateChanged(Item item) {
			System.out.println("State changed for item [" + item.getLabel() + "]");
		}
		
	}
	
	private class SimpleItemCommandListener implements ItemCommandListener {

		public void commandAction(Command c, Item item) {
			TextField textField = (TextField) item;
			// This class is only set on the text default command
			Alert alert = new Alert("Text selected");
			alert.setString("Text value:" + textField.getString());
			alert.setTimeout(Alert.FOREVER);
			display.setCurrent(alert);
		}
	}
	
	public ItemTypesMidlet() throws IOException {
		form = new Form("Form Sample");
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		form.addCommand(exitCommand);
		SampleCommandListener listener = new SampleCommandListener();
		form.setCommandListener(listener);
		display = Display.getDisplay(this);
		
		SimpleItemStateListener itemStateListener = new SimpleItemStateListener();
		form.setItemStateListener(itemStateListener);
		
		// Assemple form:
		StringItem stringItem = new StringItem("String Item Value:", "value");
		StringItem linkItem = new StringItem("Link:", "http://www.sun.com", Item.HYPERLINK);
		StringItem buttonItem = new StringItem("Button:", "Ok", Item.BUTTON);
		
		// StringItems need a default command to have the button and link look-and-feel
		linkItem.setDefaultCommand(new Command("Go", Command.ITEM, 1));
		buttonItem.setDefaultCommand(new Command("Go", Command.ITEM, 1));
		
		TextField textField = new TextField("Text field:", "initial value", 100, TextField.ANY);
		Command textFieldCommand = new Command("Select Item", Command.ITEM, 1);
		textField.setDefaultCommand(textFieldCommand);
		SimpleItemCommandListener itemCommandListener = new SimpleItemCommandListener();
		textField.setItemCommandListener(itemCommandListener);
		
		Image image = Image.createImage("/icons/iconSG.png");
		ImageItem imageItem = new ImageItem("Image item:", image, Item.LAYOUT_DEFAULT, "Alt. text if image is null");
		
		Spacer spacer = new Spacer(20, 20);
		
		DateField dateField = new DateField("Date field:", DateField.DATE);
		DateField timeField = new DateField("Time field:", DateField.TIME);
		DateField dttmField = new DateField("Date/Time field:", DateField.DATE_TIME);
		
		ChoiceGroup multipleGroup = new ChoiceGroup("Multiple", Choice.MULTIPLE);
		multipleGroup.append("Choice A", null);
		multipleGroup.append("Choice B", null);
		multipleGroup.append("Choice C", null);
		ChoiceGroup singleGroup = new ChoiceGroup("Exclusive", Choice.EXCLUSIVE);
		singleGroup.append("Choice A", null);
		singleGroup.append("Choice B", null);
		singleGroup.append("Choice C", null);
		ChoiceGroup pupupGroup = new ChoiceGroup("Pup-up", Choice.POPUP);
		pupupGroup.append("Choice A", null);
		pupupGroup.append("Choice B", null);
		pupupGroup.append("Choice C", null);
		
		Gauge gauge = new Gauge("Gauge", true, 20, 10);
		Gauge niGauge = new Gauge("NI-Gauge", false, 20, 5);
		Gauge indGauge = new Gauge("Indefinite-Gauge", false, Gauge.INDEFINITE, 0);
		
		form.append(stringItem);
		form.append(linkItem);
		form.append(spacer);
		form.append(buttonItem);
		form.append(textField);
		form.append(imageItem);
		form.append(dateField);
		form.append(timeField);
		form.append(dttmField);
		form.append(multipleGroup);
		form.append(singleGroup);
		form.append(pupupGroup);
		form.append(gauge);
		form.append(niGauge);
		form.append(indGauge);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(form);
	}

}
